<template>
  <a-table
    v-if="dataSource.length > 0"
    :dataSource="dataSource"
    :columns="columnsFilter"
    :pagination="query"
    @change="onChange"
    :loading="loading"
    :size="lineheight"
    :row-key="record => record.userId"
  >
    <template #bodyCell="{ column = {}, record }">
      <template v-if="column.key === 'operation'">
        <reset-button :data="record" />
      </template>
      <template v-if="column.key === 'gmtCreate'">
        <span>
          {{
            record.gmtCreate
              ? dayjs.utc(record.gmtCreate).format('YYYY-MM-DD HH:mm:ss')
              : ''
          }}
        </span>
      </template>
      <template v-if="column.key === 'joinTime'">
        <span>
          {{
            record.joinTime
              ? dayjs.utc(record.joinTime).format('YYYY-MM-DD')
              : ''
          }}
        </span>
      </template>
      <template v-if="column.key === 'internshipEndDate'">
        <span>
          {{
            record.internshipEndDate
              ? dayjs.utc(record.internshipEndDate).format('YYYY-MM-DD')
              : '-'
          }}
        </span>
      </template>
      <template v-if="column.key === 'userStatus'">
        <div
          style="display: flex; flex-wrap: nowrap; align-items: center"
          v-if="record.userStatus === 'job'"
        >
          <span
            :style="{
              width: '6px',
              height: '6px',
              borderRadius: '100%',
              marginRight: '8px',
              backgroundColor: '#52c41a',
            }"
          ></span>
          <div>在职</div>
        </div>
        <div
          style="display: flex; flex-wrap: nowrap; align-items: center"
          v-else-if="record.userStatus === 'intern'"
        >
          <span
            :style="{
              width: '6px',
              height: '6px',
              borderRadius: '100%',
              marginRight: '8px',
              backgroundColor: '#0086ff',
            }"
          ></span>
          <div>实习</div>
        </div>
        <div
          style="display: flex; flex-wrap: nowrap; align-items: center"
          v-else
        >
          <span
            :style="{
              width: '6px',
              height: '6px',
              borderRadius: '100%',
              marginRight: '8px',
              backgroundColor: '#d3d3d3',
            }"
          ></span>
          <div>离职</div>
        </div>
      </template>
      <template v-if="column.key === 'isActivated'">
        <span>
          <a-tag :color="record.isActivated === 1 ? 'green' : 'default'">
            {{ record.isActivated === 1 ? '已发布' : '未发布' }}
          </a-tag>
        </span>
      </template>
      <template v-if="column.key === 'userPwd'">
        <span> ********* </span>
      </template>
    </template>
  </a-table>
  <div
    v-else
    style="
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
    "
  >
    <a-spin :spinning="loading">
      <a-empty
        :image="notData"
        :description="loading ? '加载中...' : '暂无数据'"
      />
    </a-spin>
  </div>
</template>
<script setup>
import { onMounted, computed, watch } from 'vue'
import ATable from '@/components/table.vue'
import ResetButton from './table-content-pwReset.vue'
import notData from '@/assets/images/not-data.png'
import {
  useColumns,
  useQuery,
  useDataSourceManager,
  useDataSource,
  useLineheight,
  useLoading,
  useFiltersUser,
  useSortUser,
} from './provide-context.js'
import dayjs from 'dayjs'
/**
 * 全局数据
 */
let columns = useColumns()
let dataSource = useDataSource()
let filtersUser = useFiltersUser()
let sortUser = useSortUser()
let query = useQuery()
let loading = useLoading()
let lineheight = useLineheight()
let { update } = useDataSourceManager()
/**
 * 表头
 */
let columnsFilter = computed(() => {
  // 给表格列添加过滤和排序
  columns.value.forEach(e => {
    if (e.key === 'joinTime') {
      e.sortOrder =
        sortUser?.value?.columnKey === 'joinTime' && sortUser?.value?.order
    } else if (e.key === 'gmtCreate') {
      e.sortOrder =
        sortUser?.value?.columnKey === 'gmtCreate' && sortUser?.value?.order
    } else if (e.key === 'internshipEndDate') {
      e.sortOrder =
        sortUser?.value?.columnKey === 'internshipEndDate' &&
        sortUser?.value?.order
    } else if (e.key === 'userStatus') {
      e.filteredValue = filtersUser?.value?.userStatus || null
    } else if (e.key === 'isActivated') {
      e.filteredValue = filtersUser?.value?.isActivated || null
    }
  })
  return columns?.value?.filter(({ checked }) => checked) || []
})
/**
 * table组件分页、排序、筛选变化事件
 */
function onChange({ current, pageSize }, filters, sorter, { action }) {
  query.value.current = current
  query.value.pageSize = pageSize
  if (action !== 'paginate') {
    query.value.current = 1
    query.value.pageSize = 10
  }
  filtersUser.value = filters
  sortUser.value = sorter
  update()
}
/**
 * 将密码字符串转换为掩码形式
 */
const maskPassword = password => {
  return '*'.repeat(password.length)
}
//初始化调用
onMounted(() => {
  query.value.current = 1
  query.value.pageSize = 10
  update()
})
</script>
